package com.kevis.gulimall.order.listener;

import com.kevis.gulimall.order.entity.OrderEntity;
import com.kevis.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;


/**
 * Description：接收消息队列，是订单的队列，不是库存的队列
 */
@Slf4j
@Service
@RabbitListener(queues = "order.release.order.queue")
public class OrderCloseListener {

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void listener(OrderEntity entity,
                         Channel channel,
                         Message message) throws IOException {
        try {
            orderService.closeOrder(entity);
            // 手动调用支付宝收单
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            log.error(e.getMessage());
            //消息处理失败，就拒绝回去，然后会进行重发，但是如果问题不解决，就会一直重发，卡死整个程序
            //有人将其存放到数据库，定时再来发送
            //channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
            //channel.basicRecover(true);
        }
    }
}
